package com.qingzhou.web.service.impl.convert;


import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import com.alibaba.fastjson2.JSON;
import com.qingzhou.web.repository.entity.NonGeneticDetail;

import java.util.ArrayList;
import java.util.List;

public class MedicationsHistoryNamesConverter implements Converter<List<String>> {
    @Override
    public WriteCellData<?> convertToExcelData(List<String> value, ExcelContentProperty contentProperty,
                                               GlobalConfiguration globalConfiguration) {
        if(CollUtil.isEmpty(value)){
            WriteCellData<Object> emptyData = new WriteCellData<>("");
            emptyData.setType(CellDataTypeEnum.STRING);
            return emptyData;
        }

        return new WriteCellData<>(this.getConvertString(value));
    }

    public String getConvertString(List<String> value){
        List<String> rows = new ArrayList<>();
        for(String item : value){
            rows.add(StrUtil.toString(JSON.parseObject(item).get("name")));
        }
        return CollUtil.join(rows,"\n");
    }
}
